{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "0341a89e3dc0"
   },
   "source": [
    "##### Copyright 2020 The Cirq Developers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "form",
    "id": "906e07f6e562"
   },
   "outputs": [],
   "source": [
    "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "# https://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "f6f750b81e1d"
   },
   "source": [
    "# AQT Cirq Tutorial"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "dcea2d63ce2a"
   },
   "source": [
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://www.example.org/cirq/tutorials/aqt/getting_started\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on QuantumLib</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/master/docs/tutorials/aqt/getting_started.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/master/docs/tutorials/aqt/getting_started.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/tutorials/aqt/getting_started.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "bd9529db1c0b"
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    import cirq\n",
    "except ImportError:\n",
    "    print(\"installing cirq...\")\n",
    "    !pip install --quiet cirq\n",
    "    print(\"installed cirq.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "b42eeeef4398"
   },
   "source": [
    "[AQT](https://www.aqt.eu) supports Cirq as a third party software development kit and offers access to various quantum computing devices and simulators in the backend. Login to the [AQT Gateway Portal](https://gateway-portal.aqt.eu) to get a list of available devices.\n",
    "\n",
    "After the Cirq installation has finished successfully, you are ready to use different backends by the use of a token and the corresponding backend URL like in the following getting started tutorial.\n",
    "\n",
    "## Use your AQT credentials"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "4e87b1af4806"
   },
   "outputs": [],
   "source": [
    "import cirq\n",
    "from cirq.aqt.aqt_device import get_aqt_device\n",
    "from cirq.aqt.aqt_sampler import AQTSampler\n",
    "access_token = 'MY_TOKEN'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "63a64281ca4e"
   },
   "source": [
    "Where `MY_TOKEN` is your access token for a specific AQT device. You need to subscribe to an AQT backend at the [AQT Gateway Portal](https://gateway-portal.aqt.eu) and retreive the access token. Then you can access the AQT device by:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "4fbc40903531"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0───1\n"
     ]
    }
   ],
   "source": [
    "device, qubits = get_aqt_device(2)\n",
    "print(device)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "576763bf5e42"
   },
   "source": [
    "## Sample a quantum circuit\n",
    "\n",
    "You can then use that device in Cirq. For example, preparing the Bell state\n",
    "\n",
    "$$ |\\psi\\rangle=\\frac{1}{\\sqrt{2}}(|00\\rangle-\\mathrm{i}|11\\rangle) $$\n",
    "\n",
    "by writing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "37a9927421ac"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0: ───XX───────\n",
      "      │\n",
      "1: ───XX^0.5─── [cirq.LineQubit(0), cirq.LineQubit(1)]\n"
     ]
    }
   ],
   "source": [
    "circuit = cirq.Circuit(device=device)\n",
    "circuit.append([cirq.XX(qubits[0], qubits[1])**0.5])\n",
    "device.validate_circuit(circuit)\n",
    "print(circuit, qubits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8cad055ecdb1"
   },
   "source": [
    "This circuit can then be sampled on the real-hardware backend as well as on a simulator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "45cd8ccb7919"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "m=1010010001010001111001001111000110010000111000011011110110101010010101001101111100011001000000100010, 1010010001010001111001001111000110010000111000011011110110101010010101001101111100011001000000100010\n"
     ]
    }
   ],
   "source": [
    "url = 'BACKEND_URL'\n",
    "aqt_sampler = AQTSampler(url, access_token=access_token)\n",
    "aqt_sweep = aqt_sampler.run(circuit, repetitions=100)\n",
    "print(aqt_sweep)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "a6c7fd3190fe"
   },
   "source": [
    "Where `BACKEND_URL` is the API URL of the AQT backend as specified in your subscription.\n",
    "\n",
    "**Note:** At the moment, the ```run()``` method of the AQTSampler implicitly performs measurements on all qubits at the end of the circuit, so explicit measurement operations aren't required. In fact, using explicit measurements will cause the the AQTSampler to fail. More fine-grained measuement operations will be added to the AQT API in the future."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "c469db551c46"
   },
   "source": [
    "## AQT Simulators\n",
    "\n",
    "The AQT simulators are capable of running ideal simulations (without a noise model) and real simulations (with a noise model) of a quantum circuit. Using a simulator with noise model allows you to estimate the performance of running a circuit on the real hardware. Switching between the two simulation types is done by using the respective `BACKEND_URL` in above example.\n",
    "\n",
    "For running a simulation without noise model use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "fe6a1aba2db9"
   },
   "outputs": [],
   "source": [
    "url = 'https://gateway.aqt.eu/marmot/sim/'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "a0d027509bcb"
   },
   "source": [
    "whereas for a simulation with noise model use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "d9c75a68a6e2"
   },
   "outputs": [],
   "source": [
    "url = 'https://gateway.aqt.eu/marmot/sim/noise-model-1'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "13dd925bd48d"
   },
   "source": [
    "We will provide different noise models in the future, which will be listed on the subscriptions page at the [AQT Gateway Portal](https://gateway-portal.aqt.eu)."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "getting_started.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
